package com.fishsticks.model;

import junit.framework.Assert;
import android.content.Intent;
import android.test.ActivityInstrumentationTestCase2;
import android.test.TouchUtils;
import android.widget.Button;
import android.widget.EditText;

import com.fishsticks.R;
import com.fishsticks.view.LoginActivity;

public class LoginActivityTest extends ActivityInstrumentationTestCase2<LoginActivity>{
	
	private LoginActivity la;
	private EditText username, password;
	private Button b;
	
	public LoginActivityTest() {
		super(LoginActivity.class);
	}
	
	protected void setUp() throws Exception{
		super.setUp();
		Intent i = new Intent();
		setActivityIntent(i);
		la = getActivity();

	}
	
	
	protected void tearDown() throws Exception {
		la.finish();
		super.tearDown();
	}
	
	
	/**
	 * Tests to see if a valid login allows the user to login
	 */
	public void testAnEmptyLogin() {
		username = (EditText) la.findViewById(R.id.usernameFieldLoginPage);
		password = (EditText) la.findViewById(R.id.passwordFieldLoginPage);
		b = (Button) la.findViewById(R.id.loginButtonLoginPage);
		username.clearComposingText();
		password.clearComposingText();
		TouchUtils.tapView(this, b);
		Assert.assertNotNull(b);
		la.runOnUiThread(new Runnable() {
			  @Override
			  public void run() {
			    b.performClick();
			  }
			});		
		Assert.assertFalse(la.test);
	}	
	
	public void testAnInvalidLogin() {
		username = (EditText) la.findViewById(R.id.usernameFieldLoginPage);
		password = (EditText) la.findViewById(R.id.passwordFieldLoginPage);
		b = (Button) la.findViewById(R.id.loginButtonLoginPage);
		username.clearComposingText();
		password.clearComposingText();
		TouchUtils.tapView(this, username);
		sendKeys("D E F A U L T");
		TouchUtils.tapView(this, password);
		sendKeys("W R O N G");
		TouchUtils.tapView(this, b);
		Assert.assertNotNull(b);
		la.runOnUiThread(new Runnable() {
			  @Override
			  public void run() {
			    b.performClick();
			  }
			});	
		Assert.assertFalse(la.test);
	}
	
	
	/**
	 * Tests to see if a invalid login does not allow the user to login
	 */
	public void testCorrectLogin() {
		username = (EditText) la.findViewById(R.id.usernameFieldLoginPage);
		password = (EditText) la.findViewById(R.id.passwordFieldLoginPage);
		b = (Button) la.findViewById(R.id.loginButtonLoginPage);
		username.clearComposingText();
		password.clearComposingText();
		TouchUtils.tapView(this, username);
		sendKeys("D E F A U L T");
		TouchUtils.tapView(this, password);
		sendKeys("D E F A U L T");
		TouchUtils.tapView(this, b);
		Assert.assertNotNull(b);
		la.runOnUiThread(new Runnable() {
			  @Override
			  public void run() {
			    b.performClick();
			  }
			});	
		Assert.assertTrue(la.test);
	}



}

